package com.example.xiaoyuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xiaoyuan.mapper.StudentMapper;
import com.example.xiaoyuan.pojo.Admin;
import com.example.xiaoyuan.pojo.Clazz;
import com.example.xiaoyuan.pojo.LoginForm;
import com.example.xiaoyuan.pojo.Student;
import com.example.xiaoyuan.service.StudentService;
import com.example.xiaoyuan.utils.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    /**
     * 登录查询学生表
     * @param loginForm
     * @return
     */
    @Override
    public Student login(LoginForm loginForm) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", loginForm.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword()));

        Student student = baseMapper.selectOne(queryWrapper);

        return student;
    }

    @Override
    public Student getStudentById(Long userId) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userId);

        Student student = baseMapper.selectOne(queryWrapper);

        return student;
    }

    @Override
    public IPage<Student> getStudentByOpr(Page<Student> pageParam, Student student) {

        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();

        String studentName = student.getName();
        String clazzName = student.getClazzName();

        if (!StringUtils.isEmpty(studentName)){
            queryWrapper.like(Student::getName, studentName);
        }

        if (!StringUtils.isEmpty(clazzName)){
            queryWrapper.like(Student::getClazzName, clazzName);
        }
        queryWrapper.orderByAsc(Student::getId);

        Page<Student> studentPage = baseMapper.selectPage(pageParam, queryWrapper);

        return studentPage;
    }
}
